const { query } = require('./db');
async function follow(userId, followId) {
    const result = await query('INSERT INTO follows (user_id, follower_id) VALUES (?,?)', [userId, followId]);
    return result.insertId;
}

async function unfollow(userId, followId) {
    const result = await query('DELETE FROM follows WHERE user_id=? AND follower_id=?', [userId, followId]);
    return result.affectedRows;
}

async function getFollowers(userId) {
    const result = await query('SELECT * FROM users WHERE id IN ( SELECT follower_id FROM follows WHERE user_id = ? )', [userId]);
    console.log('getFollowers', result);
    return result;
}

async function getFollowings(userId) {
    const result = await query('SELECT * FROM users WHERE id IN ( SELECT user_id FROM follows WHERE follower_id = ? )', [userId]);
    console.log('getFollowings', result);
    return result;
}

module.exports = {
    follow,
    unfollow,
    getFollowers,
    getFollowings
};